package org.zkdn.modules.wlpt.basicinfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsideDeviceAll;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsideDeviceSignallamp;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsideDeviceAllService;
import org.zkdn.modules.wlpt.basicinfo.vo.DeviceVO;
import org.zkdn.modules.wlpt.basicinfo.vo.SignallampVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Optional;

/**
 * @Description: roadside_device_all
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Api(tags="roadside_device_all")
@RestController
@RequestMapping("/basicinfo/roadsideDevice/all")
@Slf4j
public class RoadsideDeviceAllController extends JeecgController<RoadsideDeviceAll, IRoadsideDeviceAllService> {

	@Autowired
	private IRoadsideDeviceAllService roadsideDeviceAllService;

	@ApiOperation(value="Signallamp-分页列表查询", notes="Signallamp-分页列表查询")
	@GetMapping(value = "/listByCondition")
	public Result<IPage<DeviceVO>> queryPageListByCondition(RoadsideDeviceSignallamp roadsideDeviceSignallamp,
																@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																HttpServletRequest req) {
		QueryWrapper<RoadsideDeviceSignallamp> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");

		Optional.ofNullable(roadsideDeviceSignallamp.getDeviceId()).ifPresent(deviceId -> {
			queryWrapper.like("deviceAlias.device_id", deviceId);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getSerialNumber()).ifPresent(serialNumber -> {
			queryWrapper.like("deviceAlias.serial_number", serialNumber);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getDeviceType()).ifPresent(deviceType -> {
			queryWrapper.eq("deviceAlias.device_type", deviceType);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getManufacturerId()).ifPresent(manufacturerId -> {
			queryWrapper.eq("deviceAlias.manufacturer_id", manufacturerId);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getPoleId()).ifPresent(poleId -> {
			queryWrapper.eq("deviceAlias.pole_id", poleId);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getCrossId()).ifPresent(crossId -> {
			queryWrapper.eq("deviceAlias.cross_id", crossId);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getRoadId()).ifPresent(roadId -> {
			queryWrapper.eq("deviceAlias.road_id", roadId);
		});
		Optional.ofNullable(roadsideDeviceSignallamp.getOnlineState()).ifPresent(onlineState -> {
			queryWrapper.eq("deviceAlias.online_state", onlineState);
		});
		IPage<DeviceVO> page = new Page<>(pageNo, pageSize);
		IPage<DeviceVO> pageList = roadsideDeviceAllService.queryPageList(page, queryWrapper);
		return Result.OK(pageList);
	}
	/**
	 * 分页列表查询
	 *
	 * @param roadsideDeviceAll
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "All-分页列表查询")
	@ApiOperation(value="All-分页列表查询", notes="All-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<RoadsideDeviceAll>> queryPageList(RoadsideDeviceAll roadsideDeviceAll,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<RoadsideDeviceAll> queryWrapper = QueryGenerator.initQueryWrapper(roadsideDeviceAll, req.getParameterMap());
		Page<RoadsideDeviceAll> page = new Page<RoadsideDeviceAll>(pageNo, pageSize);
		IPage<RoadsideDeviceAll> pageList = roadsideDeviceAllService.page(page, queryWrapper);
		return Result.OK(pageList);
	}



	/**
	 *   添加
	 *
	 * @param roadsideDeviceAll
	 * @return
	 */
	@AutoLog(value = "roadside_device_all-添加")
	@ApiOperation(value="roadside_device_all-添加", notes="roadside_device_all-添加")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_device_all:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RoadsideDeviceAll roadsideDeviceAll) {
		return roadsideDeviceAllService.saveByValidate(roadsideDeviceAll);
	}

	/**
	 *  编辑
	 *
	 * @param roadsideDeviceAll
	 * @return
	 */
	@AutoLog(value = "roadside_device_all-编辑")
	@ApiOperation(value="roadside_device_all-编辑", notes="roadside_device_all-编辑")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_device_all:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RoadsideDeviceAll roadsideDeviceAll) {
		return roadsideDeviceAllService.updateByValidate(roadsideDeviceAll);
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "roadside_device_all-通过id删除")
	@ApiOperation(value="roadside_device_all-通过id删除", notes="roadside_device_all-通过id删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_device_all:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		return roadsideDeviceAllService.deleteById(id);
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "roadside_device_all-批量删除")
	@ApiOperation(value="roadside_device_all-批量删除", notes="roadside_device_all-批量删除")
	//@RequiresPermissions("org.zkdn.modules.wlpt:roadside_device_all:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		return roadsideDeviceAllService.deleteByIds(ids);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "roadside_device_all-通过id查询")
	@ApiOperation(value="roadside_device_all-通过id查询", notes="roadside_device_all-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RoadsideDeviceAll> queryById(@RequestParam(name="id",required=true) String id) {
		RoadsideDeviceAll roadsideDeviceAll = roadsideDeviceAllService.getById(id);
		if(roadsideDeviceAll==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(roadsideDeviceAll);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param roadsideDeviceAll
    */
    //@RequiresPermissions("org.zkdn.modules.wlpt:roadside_device_all:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RoadsideDeviceAll roadsideDeviceAll) {
        return super.exportXls(request, roadsideDeviceAll, RoadsideDeviceAll.class, "roadside_device_all");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("roadside_device_all:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RoadsideDeviceAll.class);
    }

}
